import {Center} from '../center/center-model.js';
var center = new Center();
import { Shop } from '../shop/shop-model.js';
var shop = new Shop();
Page({
  data: {
    code:'',
    id:'',
    shop_name:''
  },
  onLoad: function (options) {
    this.data.code = options.p ? options.p : '';
    this.data.id = options.id ? options.id : '';
    if (!this.data.code || !this.data.id){
      wx.showModal({
        title: '提示',
        content: '链接有误',
      })
    }
    this.getInfo();
  },
  //店铺信息
  getInfo: function () {
    var that = this;
    var params ={
      id : this.data.id
    }
    shop.home(params, (data) => {
      that.setData({
        shop_name: data.shop.name
      })
    });
  },
  bindGetUserInfo: function (e) {
    if (!this.data.code) {
      wx.showModal({
        title: '提示',
        content: '链接有误',
      })
      return false;
    }
    var uid = wx.getStorageSync('uid');
    if(!uid){
      wx.showModal({
        title: '提示',
        content: '已为您自动登录，请确定重试！',
      })
      return false;
    }
   
    if (!e.detail.userInfo) {
      wx.showToast({
        title: '您拒绝了邀请',
        icon:'none'
      })
      return false;
    }
    var that = this;
    center._updateUserInfo(e.detail.userInfo, (data) => {
      if (data.user.status == 0) {
        wx.redirectTo({
          url: '/pages/home/forbidden',
        })
        return false;
      }
      wx.setStorageSync('userInfo', data.user);
      if (data.user) {
        that.setData({
          userInfo: data.user,
        })
      }
      var param = {
        code : that.data.code
      }
     
      shop.userBind(param, (res) => {
        if(res.status == 1){
          let url = wx.getStorageSync('redirect-url');
          wx.showModal({
            title: '恭喜您',
            content: '绑定成功',
            success(res) {
              if (res.confirm) {
                if (url) {
                  wx.redirectTo({
                    url: url,
                  })
                } else {
                  wx.switchTab({
                    url: '/pages/center/center',
                  })
                }
              } else if (res.cancel) {
                if (url) {
                  wx.redirectTo({
                    url: url,
                  })
                } else {
                  wx.switchTab({
                    url: '/pages/center/center',
                  })
                }
              }
            }
          })
         
        }else{
          wx.showModal({
            title: '提示',
            content: res.msg ? res.msg : '绑定失败',
          })
        }
          
      })

     
    });
  },
  cancel: function () {
    wx.navigateBack({})
  }
})